﻿//previous page id, current page id
var prevPid = '', curPid = '';
//save opened frame name
var frameArr = [];

//frame whether open
function isOpened(frmName){
    var i = 0, len = frameArr.length;
    var mark = false;
    for(i; i<len; i++){
        if(frameArr[i] === frmName){
            mark = true;
            return mark;
        }
    }
    return mark;
}

// toggle header
function showHeader(type){
    if(!type) { return; }
    
    var header = $api.dom('#header .active');
    $api.removeCls(header,'active');
    var thisHeader = $api.dom('#header .'+type);
    $api.addCls(thisHeader,'active');
}

// open tab
function openTab(type){
    
    if(type === 'user'){
    		// login
	    var uid = $api.getStorage('uid');
//		if(!uid){
//			api.openWin({
//		        name: 'userLogin',
//		        url: './html/userLogin.html',
//		        opaque: true,
//		        vScrollBarEnabled:false
//		    });
//		    return;
//		}
    }
    
    showHeader(type);
    
    var width = api.winWidth;
    var nav = $api.byId('nav');
    var navPos = $api.offset(nav);
    var header = $api.byId('header');
    var headerPos = $api.offset(header);
    var height = api.winHeight - navPos.h - headerPos.h;
    
    type = type || 'main';

    var actTab = $api.dom('#nav .active');
    $api.removeCls(actTab,'active');
    var thisTab = $api.dom('#nav .'+ type);
    thisTab = thisTab.parentNode;
    $api.addCls(thisTab,'active');

    //record page id
    prevPid = curPid;
    curPid = type;
    if(prevPid !== curPid){
    
        if(isOpened(type)){
            api.setFrameAttr({
                name: type,
                hidden: false
            });
        }else{
            api.openFrame({
                name: type,
                url: 'html/'+ type +'.html',
                bounces: true,
                opaque: true,
                vScrollBarEnabled: false,
                rect: {
                    x: 0,
                    y: headerPos.h,
                    w: width,
                    h: height
                }
            });
        }

        if(prevPid){
            api.setFrameAttr({
                name: prevPid,
                hidden: true
            });
            
        }

        if(!isOpened(type)){
            //save frame name
            frameArr.push(type);
        }   
    }   
}

// open city selector
function openCityList() {
	api.openWin({
	    name: 'citylist',
	    url: 'html/city-selector.html',
	    bounces: false,
        delay:200
    });
}

// 搜索页
function openSearch() {
	var UISearchBar = api.require('UISearchBar');
		UISearchBar.open({
    			placeholder: '请输入搜索关键字',
    			historyCount: 10,
    			showRecordBtn: true,
    			texts: {
        			cancelText: '取消',
        			clearText: '清除搜索记录'
    			},
    			styles: {
        			navBar: {
            			bgColor: '#FFFFFF',
            			borderColor: '#ccc'
        			},
        		searchBox: {
            		bgImg: '',
            		color: '#000',
            		height: 44
        		},
        		cancel: {
            		bg: 'rgba(0,0,0,0)',
            		color: '#D2691E',
            		size: 16
        		},
        		list: {
            		color: '#696969',
            		bgColor: '#FFFFFF',
            		borderColor: '#eee',
            		size: 16
        		},
        		clear: {
	            color: '#000000',
	            borderColor: '#ccc',
	            size: 16
	        }
   		 }
	 },  function(ret, err) {
    		if (ret) {
	        alert(JSON.stringify(ret));
	    } else {
	        alert(JSON.stringify(err));
	    }
	});
}

function getSteps() {
	var pedometer = api.require('pedometer');
	pedometer.getSteps(function(ret) {
	    alert(ret.steps);
	});
}

function openMainPage() {
    
	openTab('main');
	    
	var pedometer = api.require('pedometer');
	pedometer.startCount(function(ret) {
	
	});
}

apiready = function() {
	var header = $api.byId('header');
	$api.fixStatusBar(header);
	
	//status bar style
    api.setStatusBarStyle({
	    style: 'light'
	});

	if(checkToken()) {
		openMainPage();
	}

};